<?php

namespace app\merch\controller;

use app\commons\model\Member;
use app\merch\controller\Main;
use think\Controller;
use think\Db;
use QRcode;
use think\Exception;

//商家PC端
class Merchadmin extends Main {

    public function menu() {
        $menu = array(array('title' => '控制台', 'jump' => '/', "icon" => "layui-icon-console"),
            array("icon" => "layui-icon-user", 'title' => '个人信息', 'jump' => 'merchadmin/userinfo'));
        if ($this->merchinfo['isagent'] == 1) {
            $menu[] = array("icon" => "layui-icon-face-smile-b", 'title' => '商家管理',
                'jump' => $this->sysset['agent_level'] == '' ? 'agent/index' : 'agent/index1');
        }
        if ($this->merchinfo['isagent'] == 1) {
            $menu[] = array("icon" => "layui-icon-diamond", 'title' => '代理管理', 'jump' => 'agent2/index');
        }
        $menu[] = array("icon" => "layui-icon-note", 'title' => '活动管理', 'jump' => 'sale/index');
        $menu[] = array("icon" => "layui-icon-video", 'title' => '视频记录', 'jump' => 'videom/index');
        return jsonpp($menu);
    }

    public function index() {

        $_GPC = input();
        $platform_name = $this->sysset['basic']['platform_name'];
        $logo = $this->sysset['basic']['logo'];
        return $this->view->fetch();
    }

    public function userinfo(Member $member) {
        $_GPC = input();
        if ($this->request->isPost()) {
            try {
                if (isset($_GPC['agent_id']) && intval($_GPC['agent_id']) > 0) {
                    $agent = pdo_get('member', array('id' => $_GPC['data']['agent_id']));
                    $agent['num'] += $data['number'];
                    if ($agent['num'] > 0 && $agent['num'] < $_GPC['data']['number']) {
                        jsont([], 1, '代理点数剩余：' . $agent['num'] . '，点数最多只能填写：' . $agent['num'] . '！');
                    }
                }
                if ($_GPC['data']['pwd'] == '') {
                    unset($_GPC['data']['pwd']);
                } else {
                    list($msec, $sec) = explode(' ', microtime());
                    $msectime = (float) sprintf('%.0f', (floatval($msec) + floatval($sec)) * 1000);
                    $_GPC['data']['pwd'] = md5($_GPC['data']['pwd'] . $msectime);
                    $_GPC['data']['salt'] = $msectime;
                }
                $result = $member->allowField(true)->save($_GPC['data'],array('id' => $this->merchinfo['id']));//pdo_update('member', $_GPC['data'], array('id' => $this->merchinfo['id']));
                if (!empty($result) || $result === 0) {
                    jsont([],0,'修改成功！');
                } else {
                    jsont([], 1, '修改失败！');
                }

            }catch (Exception $exception){
                jsont([], 1, $exception->getMessage());
            }

        }
        $data = pdo_get('member', array('id' => $this->merchinfo['id']));
        jsont(get_upload_data($data));
    }

    public function tongji() {
        $_GPC = input();
        $this->merchinfo = pdo_get('member', array('id' => $this->merchinfo['id']));
        session('merchinfo', base64_encode(serialize($this->merchinfo)));
        $data = array();
        if ($this->merchinfo['isagent'] == 1) {
            $data['salenum'] = Db::name('member')
                            ->where('id', '<>', $this->merchinfo['id'])
                            ->where(['agent_id' => $this->merchinfo['id']])->count();
            $data['salenum1'] = Db::name('member')
                            ->where('createtime', '>', strtotime("-1 day"))
                            ->where('id', '<>', $this->merchinfo['id'])
                            ->where(['agent_id' => $this->merchinfo['id']])->count();
        } else {
            $data['salenum'] = Db::name('sale')->where(array('member_id' => $this->merchinfo['id']))->count();
            $data['salenum1'] = Db::name('sale')
                    ->where(['member_id' => $this->merchinfo['id']])
                    ->where('createtime', '>', strtotime("-1 day"))
                    ->count();
        }
        $data['mobileurl'] = $this->request->domain(true) . '/';
        $data['member_id'] = $this->merchinfo['id'];
        $data['num'] = $this->merchinfo['num'] == 0 ? '不限' : $this->merchinfo['num'];
        $data['endtime'] = $this->merchinfo['endtime'] == 0 ? '永久' : date('Y-m-d', $this->merchinfo['endtime']);
        $data['isagent'] = $this->merchinfo['isagent'];
        jsonpp($data);
    }

    public function getopenid() {
        header('content-type: image/png');
        require_once '../extend/qrcode/phpqrcode.php';
        ob_start();
        QRcode::png('https://open.douyin.com/platform/oauth/connect/?client_key='
                . $this->sysset['basic']['client_key']
                . '&response_type=code&scope=user_info,video.create&redirect_uri='
                . urlencode($this->request->domain(true) . '/web/index/getopenid'), false, 'L', 5, 2);
        $qrcode = ob_get_contents();
        ob_end_clean();
        exit($qrcode);
    }

    public function merchqr() {

        header('content-type: image/png');
        require_once '../extend/qrcode/phpqrcode.php';
        ob_start();
        QRcode::png($this->request->domain(true) .
                '/index/merch', false, 'L', 5, 2);
        $qrcode = ob_get_contents();
        ob_end_clean();
        exit($qrcode);
    }

}
